using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Zune.Forms
{
    /// <summary>
    /// A ListItem.
    /// </summary>
    public class ListItem
    {
        #region internal Events
        internal event EventHandler ItemClick;
        #endregion

        #region Private/Internal Members
        private string _text = "item";
        private object _value = null;
        private Texture2D _icon;
        internal float _itemHeight = 10;
        internal Vector2 _location;
        internal Vector2 _size;
        internal bool _isPressed = false;
        internal Color _foreColor = Color.White;
        internal Color _altForeColor = Color.White;
        internal Color _altRowBackColor = Color.Black;
        internal Color _highlightForeColor = Color.Black;
        internal Color _altHighlightForeColor = Color.Black;
        internal Color _selectRowColor = Color.White;
        internal Color _altSelectRowColor = Color.White;
        internal Color _formColor = Color.CornflowerBlue;
        internal Vector2 _parentLocation;
        internal Vector2 _parentSize;
        internal int _parentBorderWidth = 2;
        internal int _index;
        #endregion

        #region Constructors
        /// <summary>
        /// A ListItem.
        /// </summary>
        /// <param name="text">The text.</param>
        public ListItem(string text)
        {
            _text = text;
        }

        /// <summary>
        /// A ListItem.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="value">The object value.</param>
        public ListItem(string text, object value)
        {
            _text = text;
            _value = value;
        }

        /// <summary>
        /// A ListItem
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="value">The object value.</param>
        /// <param name="icon">The Texture2D icon.</param>
        public ListItem(string text, object value, Texture2D icon)
        {
            _text = text;
            _value = value;
            _icon = icon;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        public object Value
        {
            get { return _value; }
            set { _value = value; }
        }

        /// <summary>
        /// Gets or sets the Icon.
        /// </summary>
        public Texture2D Icon
        {
            get { return _icon; }
            set { _icon = value; }
        }
        #endregion

        #region Internal Methods
        internal void Draw(SpriteBatch spriteBatch, SpriteFont font)
        {
            if (_location.X >= _parentLocation.X && _location.X <= _parentLocation.X + _parentSize.X
                && _location.Y >= _parentLocation.Y - _itemHeight && _location.Y + _size.Y <= _parentLocation.Y + _parentSize.Y + _itemHeight)
            {
                Color textColor = _foreColor;
                Color selectBackColor = _selectRowColor;
                if (_index % 2 == 0)
                {
                    selectBackColor = _altSelectRowColor;
                    textColor = _altForeColor;
                    Texture2D rectRow = Zune.Forms.Drawing.CreateRectangle(spriteBatch.GraphicsDevice, (Int32)_size.X + 10, (Int32)_itemHeight, _altRowBackColor);
                    spriteBatch.Draw(rectRow, new Vector2(_location.X - 10, _location.Y), Color.White);
                }

                if (_isPressed)
                {
                    Texture2D rect = Zune.Forms.Drawing.CreateRectangle(spriteBatch.GraphicsDevice, (Int32)_size.X - 10, (Int32)_itemHeight, selectBackColor);
                    spriteBatch.Draw(rect, _location, Color.White);
                    if (_index % 2 == 0)
                    {
                        textColor = _altHighlightForeColor;
                    }
                    else
                    {
                        textColor = _highlightForeColor;
                    }
                }

                Vector2 textLocation = _location;
                if (_icon != null)
                {
                    Vector2 iconLoc = new Vector2(_location.X, _location.Y + (_size.Y / 2 - _icon.Height / 2));
                    spriteBatch.Draw(_icon, iconLoc, Color.White);
                    textLocation = new Vector2(_icon.Width + 2 + _location.X, _location.Y);
                }

                spriteBatch.DrawString(font, _text, textLocation, textColor);
            }
        }

        internal void Update(TouchLocation touch)
        {
            Vector2 p = touch.Position;
            //if (touch.State != TouchLocationState.Moved)
            if (Utilities.IsControlClicked(touch, _location, _size))
            {
                _isPressed = true;
                if (touch.State == TouchLocationState.Released)
                {
                    _isPressed = false;
                    if (ItemClick != null)
                        ItemClick(this, EventArgs.Empty);
                }
            }
            else
            {
                _isPressed = false;
            }
        }
        #endregion
    }
}